package com.tarena.lbs.basic.web.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.mapper.UserGroupMapper;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class UserGroupRepository {

    @Autowired
    private UserGroupMapper userGroupMapper;

    public BasePageList<UserGroupBO> getUserGroupByPage(UserGroupQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<UserGroupDO> userGroupDOS = userGroupMapper.selectAll(query);
        PageInfo<UserGroupDO> pageInfo = new PageInfo<>(userGroupDOS);
        BasePageList<UserGroupBO> userGroupPage = null;
        if (pageInfo.getTotal() > 0) {
            if (pageInfo.getTotal() > 0) {
                List<UserGroupDO> list = pageInfo.getList();
                List<UserGroupBO> bos = list.stream().map(aDo -> {
                    UserGroupBO bo = new UserGroupBO();
                    BeanUtils.copyProperties(aDo, bo);
                    return bo;
                }).collect(Collectors.toList());
                userGroupPage = new BasePageList<>(bos, pageInfo.getTotal());
            }else {
                //没查到
                userGroupPage = new BasePageList<>(null, 0L);
            }

        }
        return userGroupPage;
    }

    public void saveUserGroup(UserGroupBO bo) {
        UserGroupDO uDO=new UserGroupDO();
        BeanUtils.copyProperties(bo,uDO);
        userGroupMapper.insert(uDO);
    }

    public String getGroupTagsById(Integer groupId) {
        return userGroupMapper.getTagIdsById(groupId);
    }
}
